package com.geline.cloud.domain;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表格分页数据对象
 *
 * @author ruoyi
 */
public class TableDataInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 当前页数 */
    private long pageNum;
    /** 每页个数 */
    private long pageSize;
    /** 总记录数 */
    private long total;
    /** 列表数据 */
    private List<T> rows;
    /** 消息状态码 */
    private int code;
    /** 消息内容 */
    private String msg;
    /** 合计信息内容 */
    private String totalMsg = "";
    /** 顶部按钮行信息内容 */
    private String topMsg = "";
    /** 是否多表格 */
    private boolean isMoreTable = false;
    //扩展字段
    private Object extend;

    /**
     * 表格数据对象
     */
    public TableDataInfo() {
    }

    /**
     * 表格数据对象
     */
    public TableDataInfo(long pageNum, long pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    /**
     * 分页
     *
     * @param list 列表数据
     * @param total 总记录数
     */
    public TableDataInfo(List<T> list, int total) {
        this.rows = list;
        this.total = total;
    }

    public static <R> TableDataInfo<R> build() {
        TableDataInfo<R> tableDataInfo = new TableDataInfo<R>(0, 0);
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(0);
        tableDataInfo.setRows(new ArrayList<>());
        return tableDataInfo;
    }

    public static <R> TableDataInfo<R> build(IPage<R> page) {
        TableDataInfo<R> tableDataInfo = new TableDataInfo<R>(page.getCurrent(), page.getSize());
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;
    }

    public static <R> TableDataInfo<R> build(IPage<?> page, Class<R> clazz) {
        TableDataInfo<R> tableDataInfo = new TableDataInfo<R>(page.getCurrent(), page.getSize());
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(page.getTotal());
        List<R> collect = page.getRecords().stream().map(row -> BeanUtil.toBean(row, clazz)).collect(Collectors.toList());
        tableDataInfo.setRows(collect);
        return tableDataInfo;
    }

    public long getPageNum() {
        return pageNum;
    }

    public void setPageNum(long pageNum) {
        this.pageNum = pageNum;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTotalMsg() {
        return totalMsg;
    }

    public void setTotalMsg(String totalMsg) {
        this.totalMsg = totalMsg;
    }

    public String getTopMsg() {
        return topMsg;
    }

    public void setTopMsg(String topMsg) {
        this.topMsg = topMsg;
    }

    public boolean isMoreTable() {
        return isMoreTable;
    }

    public void setMoreTable(boolean moreTable) {
        isMoreTable = moreTable;
    }

    public Object getExtend() {
        return extend;
    }

    public void setExtend(Object extend) {
        this.extend = extend;
    }
}
